package com.green.javagreen.config;

import com.green.javagreen.bean.AdminUserBean;
import com.green.javagreen.service.AdminUserRepository;
import com.green.javagreen.util.MD5Util;
import com.green.javagreen.util.ReviewDateFormatUtl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 添加全权限用户
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 * Created by brioa on 2018/4/24.
 */
@Component
@Order(value = 2)
public class AdminRunner implements CommandLineRunner {
    private AdminUserRepository adminUserRepository;

	@Autowired
    public void setAdminUserRepository(AdminUserRepository adminUserRepository) {
        this.adminUserRepository = adminUserRepository;
    }


    @Override
    public void run(String... args) throws Exception {
        // 添加全权限用户
        String account1 = "superadmin";
        String name1 = "超级管理员";
        String password1 = "1234qwer";

        // 更新/保存
        saveAdmin(account1, name1, password1);
    }


    /**
     * 保存超级管理员
     *
     * @param account
     * @param name
     * @param password
     */
    private void saveAdmin(String account, String name, String password) {
        // 寻找管理员
        AdminUserBean result = adminUserRepository.findByCodeEquals(account);
        if (result == null) {
            result = new AdminUserBean();
        }
        result.setCode(account);
        result.setName(name);
        result.setPassWord(MD5Util.MD5Encode(password));
        result.setIsSuperAdmin(true);
        result.setEnable(true);
        adminUserRepository.save(result);
    }
}
